package graph;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import crawler_twitter.CrawlerUtentiByItem;

import mysql.DBCache;

public class XML_creator {
	
	public  void createXMLGraphByItem(String item){
		DBCache db = new DBCache();
		int cont =2;
		int cont2=0;
		List<String>utenti = new LinkedList<String>();
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--  An excerpt of an egocentric social network  -->\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n<graph edgedefault=\"undirected\">";
		String inizializzazione="<!-- data schema -->\n<key id=\"name\" for=\"node\" attr.name=\"name\" attr.type=\"string\"/>\n<key id=\"valutation\" for=\"node\" attr.name=\"valutation\" attr.type=\"string\"/>";
		
		String nodo ="<node id=\"1\">\n<data key=\"name\">"+item+"</data>\n<data key=\"valutation\">item</data>\n</node>";
	   utenti = db.getUtentiByItem(item);
		String end ="</graph>\n</graphml>";
	    try {
	      FileOutputStream file = new FileOutputStream("C:\\Users\\Andrea\\Desktop\\graph.xml");
	      PrintStream Output = new PrintStream(file); 
	        Output.println(head);
	        Output.println(inizializzazione);
	        Output.println(nodo);
	        for(String st:utenti){
	        	if ((cont2 % 2) == 0){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+st+"</data>\n<data key=\"valutation\">");
	        		cont++;
	        		}
	        	else Output.println(st+"</data>\n</node>");
	        	cont2++;
	        }
	        
	        for (int i=2; i<cont; i++){
	        	Output.println("<edge source=\"1\" target=\""+i+"\"></edge>");
	        }
	        
	        Output.print(end);
	      }

	     catch (IOException e) {
	      System.out.println("Errore: " + e);
	      System.exit(1);
	    }
	
	     System.out.println("File item XML creato Correttamente");
	}
	
	
	public  void createXMLGraphByUser(String utente){
		DBCache db = new DBCache();
		int cont =2;
		int cont2=0;
		List<String>item = new LinkedList<String>();
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--  An excerpt of an egocentric social network  -->\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n<graph edgedefault=\"undirected\">";
		String inizializzazione="<!-- data schema -->\n<key id=\"name\" for=\"node\" attr.name=\"name\" attr.type=\"string\"/>\n<key id=\"valutation\" for=\"node\" attr.name=\"valutation\" attr.type=\"string\"/>";
		String nodo ="<node id=\"1\">\n<data key=\"name\">"+utente+"</data>\n<data key=\"valutation\">user</data>\n</node>";
		String tmp="<node id=\"\">\n<data key=\"name\"></data>\n<data key=\"valutation\">BAD</data>\n</node>";
	    item = db.getItemByUtente(utente);
		String end ="</graph>\n</graphml>";
	    try {
	      FileOutputStream file = new FileOutputStream("C:\\Users\\Andrea\\Desktop\\graph.xml");
	      PrintStream Output = new PrintStream(file); 
	        Output.println(head);
	        Output.println(inizializzazione);
	        Output.println(nodo);
	        Output.println(tmp);

	        for(String st:item){
	        	if ((cont2 % 2) == 0){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+st+"</data>\n<data key=\"valutation\">");
	        		cont++;
	        		}
	        
	        	else Output.println(st+"</data>\n</node>");
	        	cont2++;
	        }
	        
	        for (int i=2; i<cont; i++){
	        	Output.println("<edge source=\"1\" target=\""+i+"\"></edge>");
	        }
	        
	        Output.print(end);
	      }

	     catch (IOException e) {
	      System.out.println("Errore: " + e);
	      System.exit(1);
	    }
	
	     System.out.println("File utente XML creato Correttamente");
	}
	public XML_creator() {
		// TODO Auto-generated constructor stub
	}
	public void run(){
		ShowGraph graph = new ShowGraph();
		graph.run();
	}
}